package com.sdtv.basemodule.base;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;

import com.blankj.utilcode.util.LogUtils;
import com.gyf.immersionbar.components.ImmersionFragment;
import com.sdtv.basemodule.config.life.AppManager;
import com.sdtv.basemodule.http.cookie.PersistentCookieStore;
import com.sdtv.basemodule.mvp.IView;
import com.sdtv.basemodule.mvp.presenter.PresenterDispatch;
import com.sdtv.basemodule.mvp.presenter.PresenterProviders;
import com.sdtv.basemodule.router.ARouterUtils;
import com.sdtv.basemodule.widget.gloading.Gloading;
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle3.LifecycleProvider;

import butterknife.ButterKnife;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.base
 * @ClassName: BaseFragment
 * @Description: 该类实现
 * @Author: 小郭同学
 * @CreateDate: 2019-07-09 15:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public abstract class BaseFragment extends ImmersionFragment implements IView {

    // 跟View
    public View rootView;
    public FragmentActivity mContext;
    // presenter 注册
    private PresenterProviders mPresenterProviders;
    private PresenterDispatch mPresenterDispatch;

    /**
     * 状态显示View
     */
    private Gloading.Holder holder;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = (FragmentActivity) context;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        LogUtils.d("------每次都执行么---");
        super.onActivityCreated(savedInstanceState);
        //设置presenter等的实现
        mPresenterProviders = PresenterProviders.inject(this);
        mPresenterDispatch = new PresenterDispatch(mPresenterProviders);
        mPresenterDispatch.attachView(getActivity(), this);
        mPresenterDispatch.onCreatePresenter(savedInstanceState);
        holder = null;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(getLayoutId(), container, false);
            ButterKnife.bind(this, rootView);
        } else {
            if (null != rootView.getParent()) {
                ((ViewGroup) rootView.getParent()).removeAllViews();
            }
        }
        return rootView;
    }


    /**
     * 设置布局文件ID
     *
     * @return
     */
    public abstract int getLayoutId();


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 设置titlebar
     * 状态栏颜色
     */
    @Override
    public void initImmersionBar() {
    }


    /**
     * 加载页面失败
     */
    @Override
    public void onError() {
        if (null == holder) {
            initGloadingView();
        }
        if (holder != null) {
            holder.showLoadFailed();
        }

    }

    /**
     * 显示提示信息
     */
    @Override
    public void showMessage(String msg) {

    }

    /**
     * 显示加载中View
     */
    @Override
    public void showEmptyView() {
        if (null == holder) {
            initGloadingView();
        }
        if (holder != null) {
            holder.showEmpty();
        }

    }

    /**
     * 重新加载
     */
    @Override
    public void onLoadRetry() {

    }

    @Override
    public LifecycleProvider<Lifecycle.Event> getProvider() {
        return AndroidLifecycle.createLifecycleProvider(this);
    }

    @Override
    public void initGloadingView() {
        if (null == holder) {
            holder = Gloading.getDefault().wrap(rootView).withRetry(() -> onLoadRetry());
        }
    }

    /**
     * 显示加载Dialog
     */
    @Override
    public void showLoadingDialog() {

    }

    /**
     * 隐藏加载Dialog
     */
    @Override
    public void hideLoadingDialog() {

    }

    /**
     * 页面显示时 -并且可操作
     */
    @Override
    public void onVisible() {

    }

    /**
     * 页面不可见时执行
     */
    @Override
    public void onInvisible() {

    }

    @Override
    public void showKickOutDialog(String msg) {
        TextView msgText = new TextView(getActivity());
        msgText.setText(TextUtils.isEmpty(msg) ? msg : "账号已失效或在其他地方登录，请重新登录");
        msgText.setPadding(10, 30, 10, 10);
        msgText.setGravity(Gravity.CENTER);
        msgText.setTextSize(15);
        new AlertDialog.Builder(getActivity())
                .setView(msgText)
                .setPositiveButton("确定", (dialog, which) -> {
                    dialog.dismiss();
                    PersistentCookieStore.getInstance(getActivity().getApplication()).removeAll();
                    AppManager.getInstance().exitApp();
                    ARouterUtils.navigation("/login/LoginActivity");

                })
                .show();
    }

}
